//
//  ViewController.m
//  B-72K
//
//  Created by 张春洋 on 16/3/12.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "ViewController.h"
#import "NewsViewController.h"
#import "NewsOtherViewController.h"
#import "AllPrefixHeader.pch"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
//展示的tableview
//@property (nonatomic, strong) UITableView *tableView;
//展示的collectionview
//抽屉隐藏时的frame
@property (nonatomic, assign) CGRect drawerHiddenRect;
//抽屉显示时的frame
@property (nonatomic, assign) CGRect drawerShowRect;
//展示的tableview
@property (nonatomic, strong) UITableView *tableView;
//显示的样式,有图还是简单样式.
@property (nonatomic, assign) NSInteger drawerVCShowDetailStyle;
//显示边栏方向
@property (nonatomic, assign) NSInteger drawerShowStyle;
//展示类型
@property (nonatomic, assign) NSInteger drawerShowType;
//是否有遮罩
@property (nonatomic, assign) BOOL isWithCoverView;
//需要显示的size
@property (nonatomic, assign) CGSize drawerShowSize;
//遮罩view
@property (nonatomic, strong) UIView *coverView;
//是否能移动
@property (nonatomic, assign) BOOL canMove;
//遮罩隐藏时的frame
@property (nonatomic, assign) CGRect coverViewHiddenRect;
//遮罩显示时的frame
@property (nonatomic, assign) CGRect coverViewShowRect;

@property (nonatomic, retain) NSArray *buttonArr;/**< 按钮数组 */
@property (nonatomic, retain) NSArray *valueArr;/**< 对应value */
@property (nonatomic, retain) NSArray *imageArr;/**< 按钮数组 */
@property (nonatomic, retain)NSDictionary *dataDic;

@end
static NSString *tableViewCellID = @"tableViewCell";
static NSString *collectionViewCellID = @"collectionViewCellID";




@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self dataHandler];
    
    self.view.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1];
    [self.view NightWithType:UIViewColorTypeClear];
    self.buttonArr = [NSArray arrayWithObjects:@"全部",  @"早期项目", @"B轮后",  @"资本", @"深度", @"研究", @"",@"",@"",@"",@"",@"",nil];
    self.valueArr = [NSArray arrayWithObjects:@"all", @"67", @"68", @"69", @"70", @"71", nil];
    self.imageArr = [NSArray arrayWithObjects:@"quanbu", @"xiangmu", @"b", @"gongsi", @"ziben", @"shendu", nil];
}


- (void)dataHandler
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [_tableView NightWithType:UIViewColorTypeNormal];
    self.tableView.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = NO;
    _tableView.scrollEnabled = NO;
    
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"vccell"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _buttonArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"vccell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1];
    [cell NightWithType:UIViewColorTypeNormal];
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"quanbu"];
    }else if (indexPath.row == 1){
        cell.imageView.image = [UIImage imageNamed:@"xiangmu"];
    }else if (indexPath.row == 2){
        cell.imageView.image = [UIImage imageNamed:@"b"];
    }else if (indexPath.row == 3){
        cell.imageView.image = [UIImage imageNamed:@"gongsi"];
    }else if (indexPath.row == 4){
        cell.imageView.image = [UIImage imageNamed:@"ziben"];
    }else if(indexPath.row == 5){
        cell.imageView.image = [UIImage imageNamed:@"shendu"];
    }else{
        cell.imageView.image = [UIImage imageNamed:@""];
    }
    
    cell.textLabel.text = _buttonArr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    [self showDrawerAnimationWithDuration:0.3f withDrawerRect:_drawerHiddenRect withCoverViewRect:_coverViewHiddenRect];
//    NewsViewController * vc = [NewsViewController shareDefults];
//        vc.Str =self.valueArr[indexPath.row];
////        [vc dataHandler];
    if (indexPath!= 0 && indexPath.row) {
        NewsOtherViewController *newsOther = [[NewsOtherViewController alloc] init];
        
        newsOther.titleStr = self.buttonArr[indexPath.row];
        newsOther.Str =self.valueArr[indexPath.row];
        //归档 获取地址
        NSString *path =   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
        //2.拼接地址 (按照什么路径 保存为什么文件) (数组和字典写plist)
        NSString *strpath = [path stringByAppendingPathComponent:@"a.plist"];
        NSString *strpathtwo = [path stringByAppendingPathComponent:@"b.plist"];

        [NSKeyedArchiver archiveRootObject:newsOther.Str toFile:strpath];
        [NSKeyedArchiver archiveRootObject:newsOther.titleStr toFile:strpathtwo];

        
        
        [self.delegate tiao];

        
    }
    

    
}

- (void)reloadDataAndView
{
    if (_tableView)
    {
        [_tableView reloadData];
    }
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
//返回带有导航栏的vc并算其frame规定好显示的样式
- (UINavigationController *)getDrawerWithNavigationControllerBySize:(CGSize)size forShowStyle:(DrawerViewControllerShowStyle)showStyle andShowType:(DrawerViewControllerShowType)showType withShowDetailStyle:(DrawerViewControllerShowDetailStyle)showDetailStyle isWithCoverView:(BOOL)isWithCoverView canMove:(BOOL)canMove
{
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    static UINavigationController *NC = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        NC = [[UINavigationController alloc] initWithRootViewController:self];
//        [NC.view NightWithType:UIViewColorTypeNormal];
    });
    //判断展示样式
    switch (showType)
    {
        case 0://展示为tableview
                        self.tableView.hidden = NO;
            break;
        case 1://展示为collectionView
//                        self.collectionView.hidden = NO;
            break;
    }
    //详情界面的具体样式
    switch (showDetailStyle)
    {
        case 0:
        {
                        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"vccell"];
        }
            break;
        case 1:
        {
            //            [_tableView registerClass:[DrawerTableViewCell class] forCellReuseIdentifier:tableViewCellID];
        }
            break;
    }
    //判断从哪边展示,计算好frame
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(dismissFromView:)];
    switch (showStyle)
    {
        case 0://左边
        {
            swipe.direction = UISwipeGestureRecognizerDirectionLeft;
            _drawerHiddenRect = CGRectMake(0 - size.width, 0, size.width, size.height);
            _drawerShowRect = CGRectMake(0, 0, size.width, size.height);
            _coverViewShowRect = CGRectMake(size.width, 0, screenSize.width - size.width, size.height);
        }
            break;
        case 1://右边
        {
            swipe.direction = UISwipeGestureRecognizerDirectionRight;
            _drawerHiddenRect = CGRectMake(screenSize.width, 0, size.width, size.height);
            _drawerShowRect = CGRectMake(screenSize.width - size.width, 0, size.width, size.height);
            _coverViewShowRect = CGRectMake(0, 0, screenSize.width - size.width, size.height);
        }
            break;
    }
    NSLog(@"type = %ld",showType);
    _canMove = canMove;
    _drawerShowStyle = showStyle;
    _isWithCoverView = isWithCoverView;
    _drawerVCShowDetailStyle = showDetailStyle;
    _drawerShowType = showType;
    _drawerShowSize = size;
    _coverViewHiddenRect = _drawerHiddenRect;
    NC.view.frame = _drawerHiddenRect;
    if (canMove)
    {
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [NC.view addGestureRecognizer:pan];
    }
    else
    {
        [NC.view addGestureRecognizer:swipe];
    }
    return NC;
}
//懒加载的遮罩图,在需要的时候显示
- (UIView *)coverView
{
    if (!_coverView)
    {
        _coverView = [[UIView alloc] initWithFrame:_coverViewHiddenRect];
        [self.navigationController.view.superview addSubview:_coverView];
//        [_coverView NightWithType:UIViewColorTypeNormal];
        _coverView.backgroundColor = [UIColor lightGrayColor];
        _coverView.alpha = 0.9f;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissFromView:)];
        [_coverView addGestureRecognizer:tap];
    }
    return _coverView;
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
    CGPoint setoffPoint = [pan translationInView:pan.view];
    pan.view.transform =  CGAffineTransformTranslate(pan.view.transform, setoffPoint.x, setoffPoint.y);
    [pan setTranslation:CGPointZero inView:pan.view];
    if (_drawerShowStyle == DrawerViewControllerShowStyleLeft)
    {
        if (pan.state == UIGestureRecognizerStateEnded)
        {
            if (self.navigationController.view.frame.origin.x >= [UIScreen mainScreen].bounds.size.width - self.navigationController.view.frame.size.width)
            {
                [self getDrawerWithNavigationControllerBySize:_drawerShowSize forShowStyle:DrawerViewControllerShowStyleRight andShowType:_drawerShowType withShowDetailStyle:_drawerVCShowDetailStyle isWithCoverView:_isWithCoverView canMove:_canMove];
            }
            [self showWithDuration:0.3f];
            NewsViewController *NV = [[NewsViewController alloc]init];
            NV.navigationController.navigationBarHidden = NO;
            
            
        }
    }
    else
    {
        if (pan.state == UIGestureRecognizerStateEnded)
        {
            if (self.navigationController.view.frame.origin.x <= 0)
            {
                [self getDrawerWithNavigationControllerBySize:_drawerShowSize forShowStyle:DrawerViewControllerShowStyleLeft andShowType:_drawerShowType withShowDetailStyle:_drawerVCShowDetailStyle isWithCoverView:_isWithCoverView canMove:_canMove];
            }
            NewsViewController *NV = [[NewsViewController alloc]init];
            NV.navigationController.navigationBarHidden = NO;
            
            
            [self showWithDuration:0.3f];
        }
    }
}
- (void)dismissFromView:(UISwipeGestureRecognizer *)swipeGesture
{
    
    [self hideWithDuration:0.3f];
//   NewsViewController *NV = [[NewsViewController alloc]init];
//    NV.navigationController.navigationBarHidden = NO;
    
    
}
- (void)showWithDuration:(CGFloat)timeInterval
{
    [self showDrawerAnimationWithDuration:timeInterval withDrawerRect:_drawerShowRect withCoverViewRect:_coverViewShowRect];
}

- (void)hideWithDuration:(CGFloat)timeInterval
{
    [self showDrawerAnimationWithDuration:timeInterval withDrawerRect:_drawerHiddenRect withCoverViewRect:_coverViewHiddenRect];
   NewsViewController *NV = [[NewsViewController alloc]init];
    NV.navigationController.navigationBarHidden = NO;
    
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}

- (void)showDrawerAnimationWithDuration:(CGFloat )timeInterval withDrawerRect:(CGRect)drawerRect withCoverViewRect:(CGRect)coverViewRect
{
    [UIView animateWithDuration:timeInterval animations:^{
        self.navigationController.view.frame = drawerRect;
        if (_isWithCoverView)
        {
            self.coverView.frame = coverViewRect;
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
